<template>
  <div class="container">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="小智同学"
    ></van-nav-bar>
    <div class="chat-list" ref="list">
      <div
        class="chat-item"
        :class="msg.name === 'xz' ? 'left' : 'right'"
        :key="i"
        v-for="(msg, i) in list"
      >
        <van-image v-if="msg.name === 'xz'" fit="cover" round :src="tx" />
        <div class="chat-pao">{{ msg.msg }}</div>
        <van-image v-if="msg.name === 'self'" fit="cover" round :src="photo" />
      </div>
      <!-- <div class="chat-item right">
        <div class="chat-pao">ewqewq</div>
        <van-image fit="cover" round :src="photo" />
      </div>-->
    </div>
    <div class="reply-container van-hairline--top">
      <van-field v-model="value" placeholder="说点什么..." @keyup.enter="send">
        <span @click="send()" slot="button" style="font-size: 12px; color: #999"
          >提交</span
        >
      </van-field>
    </div>
  </div>
</template>

<script>
import xzTx from '@/assets/xz.png'
import { mapState, mapGetters } from 'vuex'
// import { BASE_URL } from '@/utils/request'
import io from 'socket.io-client'

export default {
  name: 'user-chat',
  computed: {
    ...mapState(['user']),
    ...mapGetters(['photo'])
  },
  data () {
    return {
      tx: xzTx,
      // 聊天记录列表
      // 约定：小智 {name:'xz',msg:''} 自己 {name:'self',msg:''}
      list: [
        // { name: 'xz', msg: '你好' },
        // { name: 'self', msg: 'hi!' }
      ],
      // 我发的消息
      value: ''
    }
  },
  created () {
    console.log(this.$store)
    this.initWs()
  },
  methods: {
    // 发送消息
    send () {
      this.ws.emit('message', { msg: this.value, timestamp: Date.now() })
      this.list.push({ name: 'self', msg: this.value })
      this.goBottom()
      this.value = ''
    },
    // 建立ws连接
    initWs () {
      this.ws = io('http://geek.itheima.net', { transports: ['websocket'], query: { token: this.user.token } })
      this.ws.on('connect', () => {
        console.log('连接成功！')
        this.list.push({ name: 'xz', msg: '你好，有啥问题？' })
      })
      // 接收消息
      this.ws.on('message', data => {
        console.log(
          '接收消息：', data
        )
        this.list.push({ name: 'xz', msg: data.msg })
        this.goBottom()
      })
    },
    // 聊天列表滚动到底部
    goBottom () {
      this.$nextTick(() => {
        this.$refs.list.scrollTop = this.$refs.list.scrollHeight
      })
    }
  },
  beforeDestroy () {
    // 关闭连接
    this.ws.close()
  }
}
</script>

<style scoped lang='less'>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right {
  text-align: right;
  .chat-pao {
    margin-left: 0;
    margin-right: 15px;
    &::before {
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left {
  text-align: left;
  .chat-pao {
    margin-left: 15px;
    margin-right: 0;
    &::before {
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
